package com.cmccgz.udacs_landlord_webapp.main.warning.view;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.MyGridView;
import com.cmccgz.baselibrary.view.adapter.BoxingAdapter;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.Media;
import com.cmccgz.udacs_landlord_webapp.bean.WarningDetail;
import com.cmccgz.udacs_landlord_webapp.main.warning.WarningActivity;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.WarningDealContract;
import com.cmccgz.udacs_landlord_webapp.main.warning.presenters.WarningDealPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 报障处理
 *
 * @author INX
 */
public class WarningDealFragment extends BaseFragment implements View.OnClickListener,WarningDealContract.View {
    private ViewHelper viewHelper;
    private WarningActivity parentActivity;
    private WarningDetail warningDetail;
    private TextView tv_device_name;
    private TextView tv_w_desc;
    private MyGridView gv_desc;
    private TextView tv_w_state;
    private LinearLayout ll_d_type;
    private TextView tv_d_type;
    private EditText et_d_result;
    private LinearLayout ll_d_result;
    private LinearLayout ll_sn;
    private EditText et_sn;
    private int currentDealType = 0;//处理方式，0(设备维修),1(设备替换)
    private AlertDialog selectDealTypeDialog;
    private WarningDealPresenter mPresenter;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_warning_deal;
    }

    @Override
    protected void initView() {
        parentActivity = ((WarningActivity) getActivity());
        viewHelper = ViewHelper.instance(this);
        viewHelper.setTitle("回单");
        viewHelper.setRightText("完成");
        viewHelper.setAllVisable(false);
        viewHelper.setBackTextVisable(true);
        viewHelper.setTextRightVisable(true);

        tv_device_name = ((TextView) findViewById(R.id.tv_device_name));
        tv_w_desc = ((TextView) findViewById(R.id.tv_w_desc));
        gv_desc = ((MyGridView) findViewById(R.id.gv_desc));
        tv_w_state = ((TextView) findViewById(R.id.tv_w_state));
        ll_d_type = ((LinearLayout) findViewById(R.id.ll_d_type));
        tv_d_type = ((TextView) findViewById(R.id.tv_d_type));
        ll_d_result = ((LinearLayout) findViewById(R.id.ll_d_result));
        et_d_result = ((EditText) findViewById(R.id.et_d_result));
        ll_sn = ((LinearLayout) findViewById(R.id.ll_sn));
        et_sn = ((EditText) findViewById(R.id.et_sn));
        ll_d_type.setOnClickListener(this);
        initDatas();
    }

    private void initDatas() {
        Bundle arguments = getArguments();
        if (arguments == null) {
            ToastUtils.show(mContext, "参数错误！");
            parentActivity.onBackPressed();
            return;
        }
        this.warningDetail = (WarningDetail) arguments.getSerializable("WarningDetail");
        if (this.warningDetail == null) {
            ToastUtils.show(mContext, "参数错误！");
            parentActivity.onBackPressed();
            return;
        }
        //装置详情
        tv_device_name.setText(warningDetail.house_name);
        tv_w_desc.setText(warningDetail.desc);
        String w_state;
        switch (warningDetail.w_state) {
            case "1":
                w_state = "未派单";
                break;
            case "2":
                w_state = "待处理";
                break;
            case "3":
                w_state = "待确认";
                break;
            case "4":
                w_state = "完成";
                break;
            default:
                w_state = "未知";
                break;
        }
        tv_w_state.setText(w_state);

        List<Media> extra = warningDetail.media;
        final List<String> urls = new ArrayList<>();
        for (int i = 0; extra != null && i < extra.size(); i++) {
            urls.add(extra.get(i).getImagePath());
        }
        //设置grideview
        initGridView(urls);
        asyDealTypeView();
        mPresenter = new WarningDealPresenter(this);
    }

    private void initGridView(final List<String> urls) {
        if (urls==null||urls.size()==0) {
            gv_desc.setVisibility(View.GONE);
            return;
        }else
            gv_desc.setVisibility(View.VISIBLE);
        final int numColumns = 4;
        gv_desc.setNumColumns(numColumns);
        ViewTreeObserver viewTreeObserver = tv_w_desc.getViewTreeObserver();
        final boolean[] isMeasure = new boolean[]{false};
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (isMeasure[0]) {
                    return true;
                }
                isMeasure[0] = true;
                int measuredWidth = gv_desc.getMeasuredWidth();
                gv_desc.setAdapter(new BoxingAdapter(false, gv_desc, urls, measuredWidth, this, numColumns));
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                parentActivity.onBackPressed();
                break;
            case R.id.tv_right:
                showLoadingDialog("正在提交...");
                mPresenter.dealWarning(warningDetail.w_id, AppConfig.getMobile(mContext),et_d_result.getText().toString(),currentDealType+"",et_sn.getText().toString());
                break;
            case R.id.ll_d_type:
                if (selectDealTypeDialog == null) {
                    selectDealTypeDialog = new AlertDialog.Builder(mContext)
                            .setTitle("选择处理方式")
                            .setSingleChoiceItems(new String[]{"设备维修", "设备替换"}, 0, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    currentDealType = which;
                                    asyDealTypeView();
                                    dialog.dismiss();
                                }
                            }).create();
                }
                selectDealTypeDialog.show();
                break;
        }
    }

    private void asyDealTypeView() {
        tv_d_type.setText(currentDealType == 0 ? "设备维修" : "设备替换");
        ll_sn.setVisibility(currentDealType == 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void dealSuccess() {
        dismissLoadingDialog();
        ToastUtils.show(mContext,"处理成功！");
        parentActivity.refreshDetail();
        mContext.onBackPressed();
        parentActivity.refreshWarningList();
    }

    @Override
    public void dealError(String msg) {
        ToastUtils.show(mContext,msg);
        dismissLoadingDialog();
    }
}
